package per.destiny.seckill.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import per.destiny.seckill.pojo.User;
import per.destiny.seckill.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller()
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/toList", method = RequestMethod.GET)
    public String toList(Model model, @CookieValue("userTicket") String ticket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(ticket)) {
            return "/login";
        }
        User user = userService.getUserByCookie(ticket, request, response);
        if (user == null) {
            return "/login";
        }
        model.addAttribute("user", user);
        return "goodsList";
    }
}
